const App = getApp();
Page({

	data: {
		url:App.Config.basePath,
		info:[],
		cartItems: [],

	},

	showModal(message) {
		App.WxService.showModal({
			title: '友情提示',
			content: message,
			showCancel: !1,
		});
	},
	//从缓存中购物车中的数据
	onLoad: function(options) {
		var cartItems = wx.getStorageSync("cartItems")
		var total = wx.getStorageSync("total")
		var data = App.globalData.data
		this.setData({
			cartItems: cartItems,
			total: total
		})

		var mid= wx.getStorageSync("uid");

		if (mid=="") {
			this.denglu();
		} else {

			this.getMsgs1();
		}
		this.getForm1();

	},

	onShow() {



	},

	//设置输入验证
	getForm1() {
		this.WxValidate = App.WxValidate({
			username: {
				required:{
					message:'姓名：不能为空哟'
				}
			},
			phone: {
				tel:{
					message:'手机号码：请输入11位的手机号码'
				},
				required:{
					message:'手机号码：不能为空哟'
				}
			},

			address: {
				required:{
					message:'收货地址：不能为空哟'
				}
			},

		})
	},

	getMsgs1() {
		//调用数据
		let param = {
			f: 1,
			id: wx.getStorageSync("uid"),
			loadmsg: `正在加载中`
		};

		App.HttpService.getData(param, "/users_List").then(data => {
			this.setData({
				info: data
			});
		});
	},

	formSubmit1: function (e) {

		if (!this.WxValidate.checkForm(e)) { //输入验证不通过时，弹出相应的提示信息，并返回
			const error = this.WxValidate.errorList[0]
			var msg = `${error.msg}`;
			this.showModal(msg);
			return false
		}
		else {

			var data = e.detail.value;
			data = App.Tools.extend(data, {
				cartItems:JSON.stringify(this.data.cartItems),
				omoney: wx.getStorageSync("total"),
				uid:wx.getStorageSync("uid"),
				f:0
			});

			App.HttpService.saveData(data,"/orders_Add").then(data => {


				App.WxService.showToast({
					title: "支付成功!",
					icon: 'success',
					duration: 1000,
				});
				setTimeout(function () {
					wx.setStorageSync("cartItems", [])
					wx.setStorageSync("num", "0")
					wx.switchTab({
						url:"/pages/me/index"
					})
				}, 1000)


			});
		}


	},

	onChangeAddress: function () {
		var _page = this;
		wx.chooseLocation({
			success: function (res) {
				console.log(res);
				_page.setData({
					address: res.address
				});
			},
			fail: function (err) {
				console.log(err)
			}
		});
	},

	denglu(e)
	{
		this.setData({
			modalName:"Modal"
		})



	},

	login(e)
	{
		var that=this;
		wx.getUserProfile({
			desc:'获取用户相关信息',
			success: res => {
				App.globalData.userInfo = res.userInfo
				console.log(App.globalData.userInfo);

				//调用数据
				let param = {
					mname:App.globalData.userInfo.nickName,
					addr:App.globalData.userInfo.avatarUrl
				};

				//调用数据
				App.HttpService.getData(param,"/users_Add").then(data=>{

					wx.setStorageSync("uid",data[0].jieguo);

					App.WxService.showToast({
						title: "登录成功!",
						icon: 'success',
						duration: 1500,
					});
					setTimeout(function() {

					}, 1500)

					that.getMsgs1();
					this.setData({
						mid:-1,
						modalName: null
					})
				});


			}
		})
	},

})